package com.twu.biblioteca.menu;

import com.twu.biblioteca.BibliotecaApp;

import java.util.Iterator;
import java.util.List;

/**
 * @author Aaron Edwards
 * @since 24/09/13
 */
public class Menu {

    private List<IMenuItem> menuItems;

    private Menu(List<IMenuItem> menuItems) {
        this.menuItems = menuItems;
    }

    public static Menu build(List<IMenuItem> menuItems) {
        return new Menu(menuItems);
    }


    public String getMenuString(BibliotecaApp bibliotecaApp) {

        Iterator<IMenuItem> iterator = menuItems.iterator();
        StringBuilder sb = new StringBuilder();
        for (int i = 1; iterator.hasNext(); i++) {
            sb.append(String.format("%s: %s\n", i, iterator.next().getMenuTitleForUser(bibliotecaApp.getCurrentUser())));
        }
        return sb.toString();
    }

    public IMenuItem getItem(int i) {

        if (i < 0 || i >= menuItems.size()) {
            return null;
        } else {
            return menuItems.get(i);
        }

    }

    public int getSize() {
        return menuItems.size();
    }
}
